package com.diary.database;

import com.diary.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LookDiary {

    //查找正在写的，没有被标记完成的日记的大致内容
    public static List<Object[]> select_writing(String username) {

        System.out.println("查询未完成的日记");


        // 查找用户表中所有的表名、标题、创建时间
        //1.初始化数据库连接对象
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        int target = 0;
        List<Object[]> data = new ArrayList<>();


        try {
            connection = DBUtils.getConnection();

            //更新：新增了状态，通过这个查看只能查看0状态的日记
            String sql = "SELECT id, diary_title, data_time FROM `" + username + "` WHERE life=0;";

            //使用connection对象的prepareStatement     预编译sql语句
            preparedStatement = connection.prepareStatement(sql);


            //执行查询类型的sql语句
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                String id = resultSet.getString("id");
                String diary_title = resultSet.getString("diary_title");
                String data_time = resultSet.getString("data_time");
                data.add(new Object[]{id, diary_title, data_time});
            }

            return data;

        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            DBUtils.close(resultSet,preparedStatement,connection);
        }
        return data;
    }


    //查找已经写完的，标记完成的日记的大致内容
    public static List<Object[]> select_finished(String username) {

        System.out.println("查询已完成的日记");


        // 查找用户表中所有的表名、标题、创建时间
        //1.初始化数据库连接对象
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        int target = 0;
        List<Object[]> data = new ArrayList<>();


        try {
            connection = DBUtils.getConnection();

            //更新：新增了状态，通过这个查看只能查看1状态的日记,也就是查看写完了的日记
            String sql = "SELECT id, diary_title, data_time FROM `" + username + "` WHERE life=1;";

            //使用connection对象的prepareStatement     预编译sql语句
            preparedStatement = connection.prepareStatement(sql);


            //执行查询类型的sql语句
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                String id = resultSet.getString("id");
                String diary_title = resultSet.getString("diary_title");
                String data_time = resultSet.getString("data_time");
                data.add(new Object[]{id, diary_title, data_time});
            }

            return data;

        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            DBUtils.close(resultSet,preparedStatement,connection);
        }
        return data;
    }


    //查看详细的日记内容
    public static List<Object[]> look (String id,String user) {
        System.out.println("查询id为" + id + "的日记");

        //跟数据库之间的链接
        Connection connection = null;
        //执行sql语句使用的东西
        PreparedStatement preparedStatement = null;
        //获取执行后的结果
        ResultSet resultSet = null;
        //
        List<Object[]> data = new ArrayList<>();

        try {
            //使用工具类中的方法获取链接
            connection = DBUtils.getConnection();

            //因为user是表名，不能使用preparedStatement防注入，
            //这里使用`是数据库语法，在这句中两个``中间的内容被认定为是表名
            String sql = "SELECT diary_title,diary_body,data_time from `" + user + "` WHERE id=?;";


//            System.out.println(sql);
            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setInt(1, Integer.parseInt(id));

            resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                String diary_title = resultSet.getString("diary_title");
                String diary_body = resultSet.getString("diary_body");
                String data_time = resultSet.getString("data_time");

                data.add(new Object[]{diary_title, diary_body, data_time});
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            DBUtils.close(resultSet,preparedStatement,connection);
        }

        return data;
    }
}
